<?php
/**
 * Created by PhpStorm.
 * BuyUser: USER
 * Date: 2018/11/12
 * Time: 11:24
 */

namespace common\sources\write\buyuser;
use common\models\craftsman\BuyUser;
use Yii;

class BuyUserAccess implements IBuyUserAccess
{
    public function addBuyUser($data)
    {
        $insert = 'INSERT INTO buyuser (phone,password,qq,wx,reg_time,pid)
                   VALUES (:phone,:password,:qq,:wx,:reg_time,:pid)';
        $res = Yii::$app->db->createCommand($insert )->bindValues([
            ':phone' => $data['phone'],
            ':password'=>$data['password'],
            ':qq'=>$data['qq'],
            ':wx'=>$data['wx'],
            ':reg_time'=>$data['reg_time'],
            ':pid' => isset($data['pid']) ? $data['pid'] : 0,
        ])->execute();
        return $res;
    }

    public function modifyInviter($id)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET is_inviter = :is_inviter,update_time=:update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                'is_inviter' => 1,
                'id' => $id,
                'update_time' => time(),
            ])->execute();
    }

    public function UpdatePwd($data)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET password = :password,update_time = :update_time WHERE phone = :phone ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':password' => $data['newPwd'],
                ':update_time' => time(),
                ':phone' => $data['phone']
            ])->execute();

    }
    public function modifyTime($buy_id)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET login_time = :login_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':login_time' => time(),
                ':id' => $buy_id
            ])->execute();

    }
    public function modifyQQWX($buy_id,$update)
    {
        if(!empty($update['qq'])){
            $sql = 'UPDATE ' . BuyUser::tableName() . ' SET qq = :qq,update_time = :update_time WHERE id = :id ';
            return Yii::$app->db->createCommand($sql)
                ->bindValues([
                    ':qq' => $update['qq'],
                    ':update_time' => time(),
                    ':id' => $buy_id
                ])->execute();
        }
        if(!empty($update['wx'])){
            $sql = 'UPDATE ' . BuyUser::tableName() . ' SET wx = :wx,update_time = :update_time WHERE id = :id ';
            return Yii::$app->db->createCommand($sql)
                ->bindValues([
                    ':wx' => $update['wx'],
                    ':update_time' => time(),
                    ':id' => $buy_id
                ])->execute();
        }
        return false;
    }
    public function setPayPwd($buy_id,$update)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET paypwd = :paypwd,is_pay = :is_pay,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':paypwd' => $update['paypwd'],
                ':is_pay' => 1,
                ':update_time' => time(),
                ':id' => $buy_id
            ])->execute();
    }

    public function modifyFirstInviter($data)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET is_first = :is_first,update_time = :update_time WHERE phone = :phone ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':is_first' => $data['is_first'],
                ':update_time' => time(),
                ':phone' => $data['phone']
            ])->execute();
    }

    public function modifySendRecieveTime($data)
    {
        $sql = 'UPDATE ' . BuyUser::tableName() . ' SET send_receive_time = :send_receive_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':send_receive_time' => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
}